package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.ChexuanStructrue;
import com.game.majiang.config.CreateStructrue;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicPlayer;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.LogicTable;
import com.game.majiang.logic.chexuan.ChexuanPlayer;
import com.game.majiang.logic.chexuan.ChexuanTabale;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYhalltableListReq;
import com.game.server.message.proto.out.XYhalltableListRsp;
import com.game.server.message.proto.out.XYhalltableinfoStruct;
import com.game.server.system.AbstractPlayer;
import com.game.server.system.AbstractRoom;
import com.game.server.system.AbstractTable;
import org.apache.commons.beanutils.BeanUtils;

import java.util.*;

public class XYhalltableListReq extends AbstractXYhalltableListReq {

    @Override
    public void Execute(ISession session) {
    
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            int limit = 20;
            XYhalltableListRsp rsp = new XYhalltableListRsp();
            System.out.println("this.typethis.typethis.typethis.type状态："+this.type);
            rsp.setType(this.type);
            List<XYhalltableinfoStruct> halltableinfoStructs = new ArrayList<>();

            UserInfoData userInfoData = DataManager.getInstance().getMapUserData().get(client.getUserId());

            Collection<AbstractRoom> rooms = LogicServer.getInstance().getRooms();
            for (AbstractRoom room : rooms) {
                ArrayList<AbstractTable> tables = room.getTables();
                for (AbstractTable table : tables) {
                    LogicTable tab = (LogicTable) table;
                    ChexuanTabale chexuanTab = (ChexuanTabale) tab;
                    ChexuanStructrue chexuanCreate = (ChexuanStructrue) tab.getCreate();
//                    if (this.type==0){
//                        if (chexuanTab.getGamestart()==0){
//                            continue;
//                        }
//                    }
                    XYhalltableinfoStruct halltableinfoStruct = new XYhalltableinfoStruct();
                    halltableinfoStruct.setRoomid(tab.getID());
                    halltableinfoStruct.setName(tab.getCreate().getRoomname());
                    halltableinfoStruct.setCid(tab.getCreate().getBankerid());
                    halltableinfoStruct.setPi(tab.getCreate().getDifen());
                    halltableinfoStruct.setUnionid(tab.getUnionid());
                    halltableinfoStruct.setClubid(tab.getClubid());
                    halltableinfoStruct.setMaxPlayerCount(tab.getCreate().getGamesPeopleCount());
                    int tabPlayeCount = 0;
                    Collection<AbstractPlayer> players = chexuanTab.getPlayers();
                    for (AbstractPlayer player : players) {
                        LogicPlayer logplayer = (LogicPlayer) player;
                        if (logplayer.getSeatNo() >= 0 && logplayer.getSeatNo() <= 100) {
                            tabPlayeCount++;
                        }
                    }
                    halltableinfoStruct.setNowPlayerCount(tabPlayeCount);
                    halltableinfoStruct.setGamevaluetime(tab.getCreate().getGameallTime());
//                    if (tab.getGameStartTime() > 0) {
//                        halltableinfoStruct.setGamevaluetime((int) (System.currentTimeMillis() - tab.getGameStartTime()) / (1000 * 60 ));
//                    }

                    if (tab.getGameStartTime() > 0) {
                        long starttime = tab.getGameStartTime();
                        long endtime = starttime + (tab.getCreate().getGameallTime() * 60 * 1000);
                        long todaytime = System.currentTimeMillis();
                        if (endtime >= todaytime) {
                            halltableinfoStruct.setGamevaluetime(((int) (endtime - todaytime) / 1000)/60);//剩余时间
                        }
                    }
                    halltableinfoStruct.setGamestate(tab.getGameStartTime() > 0 ? 1 : 0);
                    halltableinfoStruct.setDijiuwang(chexuanCreate.getDijiuwang());
                    ChexuanPlayer chexuanplayer = (ChexuanPlayer) chexuanTab.getPlayer(client.getUserId());

                    halltableinfoStruct.setIsDairu(0);
                    if (chexuanplayer != null && chexuanplayer.getUsebobo() > 0) {
                        halltableinfoStruct.setIsDairu(1);
                    }

                    halltableinfoStruct.setGamealltime(tab.getCreate().getGameallTime());
                    if (tab.getGameStartTime() > 0) {
                        halltableinfoStruct.setCreatetime(tab.getGameStartTime() + "");
                    }
                    Set<Integer> clubids = userInfoData.getClubids();
                    if ( halltableinfoStruct.getClubid() !=0 && clubids.size()>0){
                        for (Integer clubid : clubids) {
                            if (this.type == 1 && tab.getClubid() == 0) {
                                continue;
                            }
                            if (this.type == 2 && clubid.intValue() != this.cid) {
                                continue;
                            }
                            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
                            if (clubInfo.getUnionid() != 0 && tab.getClubid() != 0 && clubInfo.getUnionid() == tab.getUnionid()) {
                                XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(clubInfo.getUnionid());
                                if (unionInfo != null) {
                                    List<Integer> unionInfoClubids = unionInfo.getClubids();
                                    for (Integer unionInfoClubid : unionInfoClubids) {
                                        if (this.type == 2 && unionInfoClubid.intValue() != this.cid) {
                                            continue;
                                        }
                                        XyClubInfo unionClubInfo = DataManager.getInstance().getMapClub().get(unionInfoClubid);

                                        XyClubMemberInfo memberInfo = unionClubInfo.getPlayer().get(client.getUserId());
                                        if (memberInfo != null && memberInfo.getDel_type() == 0) {
                                            boolean isnext = true;
                                            for (XYhalltableinfoStruct struct : halltableinfoStructs) {
                                                if (struct.getClubid().intValue() == unionClubInfo.getId() && struct.getRoomid().intValue() == tab.getID()) {
                                                    isnext = false;
                                                    break;
                                                }
                                            }
                                            if (isnext) {
                                                halltableinfoStruct.setType(2);
                                                halltableinfoStruct.setClubid(unionClubInfo.getId());
                                                halltableinfoStruct.setHead(unionClubInfo.getHeadurl());
                                                halltableinfoStruct.setClubName(unionClubInfo.getName());
                                                XYhalltableinfoStruct cop = new XYhalltableinfoStruct();
                                                try {
                                                    BeanUtils.copyProperties(cop, halltableinfoStruct);
                                                    halltableinfoStructs.add(cop);
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    }
                                }
                            } else {
                                if (tab.getClubid() == 0 && tab.getUnionid() == 0 && this.type != 2) {
                                    boolean isnext = true;
                                    for (XYhalltableinfoStruct struct : halltableinfoStructs) {
                                        if (struct.getRoomid().intValue() == tab.getID() && struct.getClubid() == 0) {
                                            isnext = false;
                                            break;
                                        }
                                    }
                                    if (isnext) {
                                        halltableinfoStruct.setType(1);
                                        halltableinfoStruct.setHead(userInfoData.getHead());

                                        XYhalltableinfoStruct cop = new XYhalltableinfoStruct();
                                        try {
                                            BeanUtils.copyProperties(cop, halltableinfoStruct);
                                            halltableinfoStructs.add(cop);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                } else if (tab.getClubid() != 0 && tab.getUnionid() == 0 ) {
                                    boolean isnext = true;
                                    for (XYhalltableinfoStruct struct : halltableinfoStructs) {
                                        if (struct.getClubid().intValue() == clubInfo.getId() && struct.getRoomid().intValue() == tab.getID()) {
                                            isnext = false;
                                            break;
                                        }
                                    }
                                    if (tab.getClubid()!=clubInfo.getId()){
                                        isnext = false;
                                    }
                                    if (isnext) {
                                        halltableinfoStruct.setType(0);
                                        halltableinfoStruct.setClubid(clubInfo.getId());
                                        halltableinfoStruct.setHead(clubInfo.getHeadurl());
                                        halltableinfoStruct.setClubName(clubInfo.getName());
                                        XYhalltableinfoStruct cop = new XYhalltableinfoStruct();
                                        try {
                                            BeanUtils.copyProperties(cop, halltableinfoStruct);
                                            halltableinfoStructs.add(cop);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }
                    }else{
                        if (this.type == 0){
                         //   if (tab.getCreate().getBankerid() == client.getUserId() || halltableinfoStruct.getIsDairu()==1){
                            System.out.println("halltableinfoStruct.getIsDairu():"+halltableinfoStruct.getIsDairu());

                            if ( 1==1){
                                halltableinfoStruct.setType(0);
                                halltableinfoStruct.setClubid(0);
                                halltableinfoStruct.setHead(client.getVo().getHead());
                                halltableinfoStruct.setClubName("");
                                XYhalltableinfoStruct cop = new XYhalltableinfoStruct();
                                try {
                                    BeanUtils.copyProperties(cop, halltableinfoStruct);
                                    halltableinfoStructs.add(cop);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }

            rsp.setPage(this.page);
            int sumCount = halltableinfoStructs.size();
            int sumPage = (int) Math.ceil((double) sumCount / limit);
            page = page < 1 ? 1 : page > sumPage ? sumPage : page;
            page = page < 1 ? 1 : page;
            int beginIndex = ((page - 1) * limit);

            rsp.setAllCount(sumCount);
            rsp.setAllpage(sumPage);
            halltableinfoStructs.sort(Comparator.comparing(XYhalltableinfoStruct::getGamestate).thenComparing(XYhalltableinfoStruct::getGamevaluetime));


            List<XYhalltableinfoStruct> sendList = new ArrayList<>();
            int endIndex = 1;
            for (int i = 0; i < halltableinfoStructs.size(); i++) {
                if (i >= beginIndex && endIndex <= limit) {
                    XYhalltableinfoStruct eml = halltableinfoStructs.get(i);
                    sendList.add(eml);
                    endIndex++;
                }
            }

            for (XYhalltableinfoStruct xYhalltableinfoStruct : sendList) {
                xYhalltableinfoStruct.setMyclublv(3);
                XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(xYhalltableinfoStruct.getUnionid());
                if (unionInfo!=null){
                    List<Integer> clubids = unionInfo.getClubids();
                    for (Integer clubid : clubids) {
                        XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
                        if (clubInfo!=null){
                            XyClubMemberInfo memberInfo = clubInfo.getPlayer().get(client.getUserId());
                            if (memberInfo!=null && memberInfo.getIsManager()<xYhalltableinfoStruct.getMyclublv()){
                                xYhalltableinfoStruct.setMyclublv(memberInfo.getIsManager());
                            }
                        }
                    }
                }
            }

            //list转数组
            XYhalltableinfoStruct[] halltables = sendList.toArray(new XYhalltableinfoStruct[0]);
            rsp.setTableinfos(halltables);
            client.Send(rsp);
        }
    }

}
